/**
 * Created by baby520 on 2016/7/6.
 */
;!function(window,undefined){
	window.formatDate=function(now,num){

		var   year=now.getFullYear();
		var   month=now.getMonth()+1;
		var   date=now.getDate();
		var   hour=now.getHours();
		var   minute=now.getMinutes();
		var   second=now.getSeconds();
		var milliseconds=now.getMilliseconds();

		if (month<10)month = '0'+month;
		if (date<10)date = '0'+date;
		if (hour<10)hour = '0'+hour;
		if (minute<10)minute = '0'+minute;
		if (second<10)second = '0'+second;
		if (milliseconds<10){
			milliseconds = '00'+milliseconds;
		}else if(milliseconds <100){
			milliseconds = '0'+milliseconds;
		}


		if(num==1){
			//if(minute<10){
			//    minute='0'+ minute;
			//}
			return   year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second+":"+milliseconds;
		}else if(num==2){
			return  year+""+month+""+date;
		}else if(num==3){
			return  month+"-"+date;
		}else if(num==5){
			return  year+"."+month+"."+date;
		}else{
			return  year+"-"+month+"-"+date;
		}
	}
	window.getJsonp=function(api,params,callback,fn){
		var options = {
			async: false,
			dataType: 'jsonp',
			jsonp:'callback',
			jsonpCallback:callback,
			url: api,
			data: params,
			beforeSend :function(xmlHttp){
				xmlHttp.setRequestHeader("If-Modified-Since","0");
				xmlHttp.setRequestHeader("Cache-Control","no-cache");
			},
			success: fn || function(){},
			error:function(e){
				console.log(e)
				alert("请求失败");
			}
		};
		return $.ajax(options);
	};
}(window);


/**
 * 声明自定义触摸事件：移动端单击事件
 * @author Elijah
 * @date 2016-07-08
 */
$.event.special.tap = {
		/**
		 *初始化事件处理器 - this指向被绑定的元素
         * @param 附加的数据 
         * @param 事件类型命名空间 
         * @param 回调函数 
		 */
		setup:function(data, namespaces, eventHandle){
			var flag=false;
			$.event.add(this,'touchstart touchmove touchend',function(e){
				switch(event.type) {
				case 'touchstart':
					flag = false;
					break;
				case 'touchmove':
					flag = true;
					break;
				case 'touchend':
					if(flag)break;
					$.event.trigger('tap', null, e.target);
					break;
				}
			});
		},
		/** 
         * 卸载事件处理器 - this指向被绑定的元素 
         * @param 事件类型命名空间 
         */  
		teardown:function(namespaces){
			$.event.remove(this, 'touchstart touchmove touchend');
		}
};
/**
 * 声明快捷方式：$(element).tap(function(){})
 * @author Elijah
 * @date 2016-07-08
 * @param 回调函数
 */
$.fn.tap = function (callback) {  
        return this.on('tap', callback);  
};


jQuery.extend({
	/**
	 * 获取URL参数
	 * @author Elijah
	 * @date 2016-07-09
	 * @param 参数名
	 */
	getUrlParam : function(name){
		var reg = new RegExp("(\\?|&)" + name + "=([^(&|#)]*)(#|&|$)");
		var r = window.location.href.match(reg);
		if (r != null)return r[2];
		return null;
	}
});

